package org.judo.generate.mvc;

public class ModelParser implements Parser
{
	ModelResult result = new ModelResult();
	
	String curStatement;
	ParseResults parseResults;
	ModelResult modelResult = new ModelResult();
	PropParser propParser;
	
	public ParseResults getParseResults()
	{
		return parseResults;
	}

	public void setParseResults(ParseResults parseResults)
	{
		this.parseResults = parseResults;
	}

	public Parser getNestedParser()
	{
		return propParser;
	}

	public boolean isFinished()
	{
		if (curStatement != null)
			if (curStatement.equals("/model"))
				return true;
				
		return false;
	}

	public boolean needsNestedParser()
	{
		if (propParser != null)
			return true;
		
		return false;
	}

	public void parseNextLine(String line)
		throws ParseException
	{
		curStatement = line;
		propParser = null;
		
		if (line.startsWith("/model"))
		{
			if (modelResult.pluralDisplay == null)
				modelResult.pluralDisplay = modelResult.name;
			
			if (modelResult.singularDisplay == null)
				modelResult.singularDisplay = modelResult.name;
		}
		else if (line.startsWith("singular:"))
		{
			String split[] = line.split(":");
			
			if (split.length == 1)
				throw new ParseException("Singular name not specified in init line");
			
			if (split[1].equals(""))
				throw new ParseException("Singular name not specified in init line");
			
			modelResult.singularDisplay = split[1]; 
		}
		else if (line.equals(""))
		{
		}
		else if (line.startsWith("plural:"))
		{
			String split[] = line.split(":");
			
			if (split.length == 1)
				throw new ParseException("Singular name not specified in init line");
			
			if (split[1].equals(""))
				throw new ParseException("Singular name not specified in init line");
			
			modelResult.singularDisplay = split[1];
		}
		else if (line.startsWith("props:"))
		{
			propParser = new PropParser();
			propParser.setModelResult(modelResult);
		}
		else
			throw new ParseException("Unexpected tokens");
	}

	public void setInitLine(String line)
		throws ParseException
	{
		modelResult = new ModelResult();
		
		String split[] = line.split(":");
		
		if (split.length == 1)
			throw new ParseException("Model name not specified in init line");
		
		if (split[1].equals(""))
			throw new ParseException("Model name not specified in init line");
		
		modelResult.name = split[1].trim();
		parseResults.models.put(modelResult.name,modelResult);
	}

}
